<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LoginAdmin extends CI_Controller {

	function __construct(){
		parent:: __construct();
		$this->load->library('form_validation');
		$this->load->model('mod_khmerhr_admin');
	}
	function index(){
		if($this->session->userdata("admin_loggedIn")){
			redirect("admin_khmerhr/viewEmployee");

		}else{ 
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			$this->form_validation->set_rules('username', 'User Name', 'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->load->view("adminhr/login");
			}else{
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				$remember = $this->input->post('remember');
				$login = $this->mod_khmerhr_admin->getAdmin($username,MD5($password));
				if($login){
					$ses_arr = array(
						"first_name"=>$login->first_name,
						"userAdmin"=>$login->first_name.' '.$login->last_name,
						"emailAdmin"=>$login->email,
						"userId"=>$login->id,
						"userTypeId"=>$login->group_id,
						"admin_loggedIn"=>true
					);
					$this->session->set_userdata($ses_arr);
					redirect("admin_khmerhr/viewEmployee");
				}else{
					echo "<script>alert('User Name or Password invalid!');</script>"; 
					$this->load->view("adminhr/login");
				}
			}
		}


	}
}